package infostroy.bookagolf.UI;

import infostroy.bookagolf.common.Constants.Colors;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.geom.GeneralPath;

/**
 * Created by valer_000 on 2/27/14.
 */
public class GolfRoundedButtonUI extends BasicButtonUI {

    private static final int rounding = 8;

    private boolean roundedTopLeft = false;
    private boolean roundedTopRight = false;
    private boolean roundedBottomLeft = false;
    private boolean roundedBottomRight = false;

    private boolean hasShadow = true;

    private int topMargin = 0;
    private int rightMargin = 0;
    private int bottomMargin = 0;
    private int leftMargin = 0;

    private Color background = Colors.WHITE;
    private Color shadowStart = Colors.TEXT_SHADOW_START;
    private Color shadowEnd = Colors.TEXT_SHADOW_END;

    public GolfRoundedButtonUI(boolean roundedTopLeft, boolean roundedTopRight
            , boolean roundedBottomLeft, boolean roundedBottomRight, boolean hasShadow) {
        this.roundedTopLeft = roundedTopLeft;
        this.roundedTopRight = roundedTopRight;
        this.roundedBottomLeft = roundedBottomLeft;
        this.roundedBottomRight = roundedBottomRight;
        this.hasShadow = hasShadow;
    }

    public void setMargin (int topMargin, int rightMargin, int bottomMargin, int leftMargin) {
        this.topMargin = topMargin;
        this.rightMargin = rightMargin;
        this.bottomMargin = bottomMargin;
        this.leftMargin = leftMargin;
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        Graphics2D graphics = (Graphics2D)g;
        Object rh = graphics.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        ButtonModel model = ( ( AbstractButton ) c ).getModel ();

        GeneralPath path = new GeneralPath();

        if (roundedTopLeft) {
            path.moveTo(leftMargin + rounding / 2, topMargin);
            path.quadTo(leftMargin, topMargin, rounding / 2, topMargin);
        } else {
            path.moveTo(leftMargin, topMargin);
        }
        if (roundedTopRight) {
            path.lineTo(c.getWidth() - rounding / 2 - rightMargin, topMargin);
            path.quadTo(c.getWidth() - rightMargin, topMargin, c.getWidth() - leftMargin, rounding / 2 + topMargin);
        } else {
            path.lineTo(c.getWidth() - rightMargin, topMargin);
        }
        if (roundedBottomRight) {
            path.lineTo(c.getWidth() - rightMargin, c.getHeight() - rounding / 2 - bottomMargin);
            path.quadTo(c.getWidth() - rightMargin, c.getHeight() - bottomMargin
                    , c.getWidth() - rounding / 2 - rightMargin, c.getHeight() - bottomMargin);
        } else {
            path.lineTo(c.getWidth() - rightMargin, c.getHeight() - bottomMargin);
        }
        if (roundedBottomLeft) {
            path.lineTo(rounding / 2 - leftMargin, c.getHeight() - bottomMargin);
            path.quadTo(leftMargin, c.getHeight() - bottomMargin, leftMargin
                    , c.getHeight() - rounding / 2 - bottomMargin);
        } else {
            path.lineTo(leftMargin, c.getHeight() - bottomMargin);
        }
        if (roundedTopLeft) {
            path.lineTo(leftMargin, rounding / 2 + topMargin);
        } else {
            path.lineTo(leftMargin, topMargin);
        }

        graphics.setColor(background);
        graphics.fill(path);

        if (hasShadow) {
            GradientPaint gradient = new GradientPaint(0, 0, shadowStart, 0, 5, shadowEnd);
            graphics.setPaint(gradient);
            graphics.fill(path);
        }

        if ( model.isPressed () )
        {
            graphics.translate ( 1, 1 );
        }

        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, rh);
        super.paint(g, c);
    }
}
